/******************************************************************************/
package jamie.org.sil.www.usxUtils;

import java.util.TreeMap;



/******************************************************************************/
/**
   Numerical processor to handle number systems which are basically like the
   standard European system, but which use different glyphs (and possibly
   a different number base) -- ie which are positional, and are written with
   the most significant digit at the left.
   <p>

   For samples of Persian text see http://www.omniglot.com/writing/persian.htm.
   <p>

   !!! Not sure whether I need the glyphToInteger mapping table -- it seems to
   be unreliable, because in Persian (for example) there are two characters for
   3 both with the same glyph, and this confuses things.  There seems, in fact
   to be a much more reliable way of handling the conversion, as below.
   <p>
*/

public class NumeralProcessor_Positional_Ltr extends NumeralProcessor
{
  /****************************************************************************/
  /****************************************************************************/
  /**                                                                        **/
  /**                               Public                                   **/
  /**                                                                        **/
  /****************************************************************************/
  /****************************************************************************/

  /****************************************************************************/
  /**
     Initialise the new instance.  You should never need to call this directly,
     because normally I expect all instances to be created via the
     NumeralProcessorFactory class.

     @param integerToGlyph Decimal number to vernacular character mapping.

     @param textDir LTR / RTL etc.

     @param base Number base.
  */

  @Override public void initialise (TreeMap<Integer, String> integerToGlyph,
                                    String textDir,
                                    int base)
  {
    super.initialise(integerToGlyph, textDir, base);
    m_Base = integerToGlyph.size();
  }

  
  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public int fromVernacular (String n)
  {
    int res = 0;
    for (int i = 0; i < n.length(); ++i)
    {
      res *= m_Base;
      //res += m_GlyphToInteger.get(n.substring(i, i+1));
      res += Character.digit(n.charAt(i), 10);
    }

    return res;
  }


  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public void setInstance ()
  {
    m_Instance = this;
  }


  /****************************************************************************/
  /**
     @{inheritDoc}
  */

  @Override public String toVernacular (int n)
  {
    if (0 == n) return m_IntegerToGlyph.get(0);

    String res = "";
    while (n > 0)
    {
      res = m_IntegerToGlyph.get(n % m_Base) + res;
      n /= m_Base;
    }

    return res;
  }
}
